package st.business.tepth.business.visitor;

import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.FactoryInfo;
import st.business.tepth.business.bean.LocationBean;
import st.business.tepth.manager.UserBean;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-05 16:53
 */

public class VisitorPresenter extends VisitorContract.Presenter {
    private final VisitorContract.View mView;
    private final VisitorContract.Model mModel;

    public VisitorPresenter(VisitorContract.View view) {
        mView = view;
        mModel = new VisitorModel();
    }

    @Override
    void getLocationInfo(String id) {
        addDisposable(mModel.getLocationInfo(id, new OnDataConvertListener<LocationBean>() {
            @Override
            public void convert(LocationBean locationBean) {
                if (mView != null) {
                    mView.showLocationInfo(locationBean);
                }
            }

            @Override
            public void error(int code, String msg) {
                if (mView != null) {
                    mView.showError(msg);
                }
            }
        }));
    }

    @Override
    void getFactoryInfo(String androidId) {
        addDisposable(mModel.getFactoryInfo(androidId,
                new OnDataConvertListener<FactoryInfo>() {
                    @Override
                    public void convert(FactoryInfo factoryInfo) {
                        if (mView != null) {
                            mView.verificationFactoryInfo(factoryInfo);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }, new OnDataConvertListener<LocationBean>() {
                    @Override
                    public void convert(LocationBean locationBean) {
                        if (mView != null) {
                            mView.showLocationInfo(locationBean);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }));
    }

    @Override
    void setFactoryInfo(String androidId, int factoryId,
                        int groupId, String factoryName) {
        addDisposable(mModel.setFactoryInfo(androidId, factoryId,groupId, factoryName,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        mView.setInfoSucceed("绑定成功！");
                    }

                    @Override
                    public void error(int code, String msg) {
                        mView.showError(msg);
                    }
                },
                new OnDataConvertListener<LocationBean>() {
                    @Override
                    public void convert(LocationBean locationBean) {
                        if (mView != null) {
                            mView.showLocationInfo(locationBean);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }));
    }

    @Override
    void login(String account, String password, int type) {
        addDisposable(mModel.login(account, password, type, new OnDataConvertListener<UserBean>() {
            @Override
            public void convert(UserBean userBean) {
                mView.succeed(userBean);
            }

            @Override
            public void error(int code, String msg) {
                mView.showError(msg);
            }
        }));
    }
}
